/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jpa;

import entidades.InformeReferenciaDCAr;
import entidades.InformeReferenciaDCArPK;
import java.util.List;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.transaction.UserTransaction;
import jpa.exceptions.NonexistentEntityException;
import jpa.exceptions.PreexistingEntityException;
import jpa.exceptions.RollbackFailureException;

/**
 *
 * @author dvasquez
 */
public class InformeReferenciaDCArJpaController {
    @Resource
    private UserTransaction utx = null;
    @PersistenceUnit(unitName = "LepucvWebPU")
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(InformeReferenciaDCAr informeReferenciaDCAr) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (informeReferenciaDCAr.getInformeReferenciaDCArPK() == null) {
            informeReferenciaDCAr.setInformeReferenciaDCArPK(new InformeReferenciaDCArPK());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            em.persist(informeReferenciaDCAr);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findInformeReferenciaDCAr(informeReferenciaDCAr.getInformeReferenciaDCArPK()) != null) {
                throw new PreexistingEntityException("InformeReferenciaDCAr " + informeReferenciaDCAr + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(InformeReferenciaDCAr informeReferenciaDCAr) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            informeReferenciaDCAr = em.merge(informeReferenciaDCAr);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                InformeReferenciaDCArPK id = informeReferenciaDCAr.getInformeReferenciaDCArPK();
                if (findInformeReferenciaDCAr(id) == null) {
                    throw new NonexistentEntityException("The informeReferenciaDCAr with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(InformeReferenciaDCArPK id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            InformeReferenciaDCAr informeReferenciaDCAr;
            try {
                informeReferenciaDCAr = em.getReference(InformeReferenciaDCAr.class, id);
                informeReferenciaDCAr.getInformeReferenciaDCArPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The informeReferenciaDCAr with id " + id + " no longer exists.", enfe);
            }
            em.remove(informeReferenciaDCAr);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<InformeReferenciaDCAr> findInformeReferenciaDCArEntities() {
        return findInformeReferenciaDCArEntities(true, -1, -1);
    }

    public List<InformeReferenciaDCAr> findInformeReferenciaDCArEntities(int maxResults, int firstResult) {
        return findInformeReferenciaDCArEntities(false, maxResults, firstResult);
    }

    private List<InformeReferenciaDCAr> findInformeReferenciaDCArEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from InformeReferenciaDCAr as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public InformeReferenciaDCAr findInformeReferenciaDCAr(InformeReferenciaDCArPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(InformeReferenciaDCAr.class, id);
        } finally {
            em.close();
        }
    }

    public List<InformeReferenciaDCAr> findInformeReferenciaDCArById(int parameter){
        EntityManager em = getEntityManager();
        try{
         Query q = em.createNamedQuery("InformeReferenciaDCAr.findById");
         q.setParameter("id", parameter);
         return q.getResultList();
        }finally{
         em.close();
        }

    }

    public int getInformeReferenciaDCArCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from InformeReferenciaDCAr as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
